var rootPath=null;
var img = $("#progressImgage");    
var mask = $("#maskOfProgressImage");
var studentid
var seconds = 0;
var timehwnd=setInterval(function(){
    seconds += 1;
    var aa=5-seconds;
    document.getElementById('topay').disabled = true;
    document.getElementById('topay').value="请仔细核对信息("+aa+")";
    if(seconds == 5){
      document.getElementById('topay').disabled = false;
      document.getElementById('topay').value="确认付款";
      clearInterval(timehwnd);
       $("#topay").click(function(){
       		document.getElementById('topay').disabled = true;
       		document.getElementById('topay').value="支付中...";
			btn(studentid);
		});
    }
  },1000);
$(document).ready(function(){  
	rootPath=getRootPath();   
	schoolOption();
	studentid = window.sessionStorage.getItem("id");
}); 
	// $("#select_id").append("<option value='Value'>Text</option>"); 
 function schoolOption(){
	var iidd = window.sessionStorage.getItem("id");
	var school="";
	var major="";
	var direction="";
	var price="";
	var cardid="";
	var ksid="";
	var name="";
	 $.ajax({
		    url : rootPath+"/getStudentById",
		    type : "get",
		    dataType : "json",
		    data : {
		    	Id:iidd
		    },
		    cache : false,
		    async : false,
		    success : function(requestJson) {
		    	var requestStudentArray=requestJson.extend.student;
		    	 school=requestStudentArray.school;
		    	 major=requestStudentArray.major;
		    	 direction=requestStudentArray.direction;
		    	 price=requestStudentArray.price/100;
		    	 name=requestStudentArray.name;
		    	 cardid=requestStudentArray.cardId;
		    	 ksid=requestStudentArray.examineeId;
		    	 var pay=requestStudentArray.payType;
		    	 if(pay==1){
		    		 $("#topay").hide();
		    		 $("#pay-done").show();
		    	 }else{
		    	 	$("#topay").show();
		    		$("#pay-done").hide();
		    	 }
		    	}
		});
 
	//alert(school+".."+major+".."+direction+".."+price);
	if(direction!=""&&direction!=null){
		document.getElementById("zyfx").style.display="block "; 
		document.getElementById("school").innerHTML=school;
	 	document.getElementById("fushi").innerHTML=major;
	 	document.getElementById("zhuanyefangxaing").innerHTML=direction;
	 	document.getElementById("jine").innerHTML=price;
	 	document.getElementById("name").innerHTML=name;
	 	document.getElementById("ksid").innerHTML=ksid;
	 	document.getElementById("cardid").innerHTML=cardid;
	}else{
		document.getElementById("school").innerHTML=school;
	 	document.getElementById("fushi").innerHTML=major;
	 	document.getElementById("jine").innerHTML=price;
	 	document.getElementById("name").innerHTML=name;
	 	document.getElementById("ksid").innerHTML=ksid;
	 	document.getElementById("cardid").innerHTML=cardid;
	}
}
 
 function btn(studentid){
	var iidd = window.sessionStorage.getItem("id");
	 $.ajax({
		    url : rootPath+"/getStudentById",
		    type : "get",
		    dataType : "json",
		    data : {
		    	Id:iidd
		    },
		    cache : false,
		    async : false,
		    success : function(requestJson) {
		    	var requestStudentArray=requestJson.extend.student;
		    	 var pay=requestStudentArray.payType;
		    	 if(pay==1){
		    		 $("#topay").hide();
		    		 $("#pay-done").show();
		    	 }else{
		    	 	$("#topay").show();
		    		$("#pay-done").hide();
		    		topay(studentid);
		    	 }
		    	}
		});
	//alert(school+".."+major+".."+direction+".."+price);
}
 
 var utils = {
		
	/**
	 * 
	 * @param paramName
	 * @returns
	 */
	getParam : function(paramName){
		var url = decodeURI(location.href); 
        var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");  
        var paraObj = {}; 
        for (var i = 0; j = paraString[i]; i++) {  
            paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);  
        }  
        var returnValue = paraObj[paramName.toLowerCase()];  
        if (typeof (returnValue) == "undefined") {  
            return "";  
        } else {  
            return returnValue;  
        }  
	},
	
	/**
	 * 用法
	 * utils.getParam("参数名"); 
	 */
	
};
 
function onBridgeReady(json) {
	var orderidss=json.orderid;
	var appId=json.appId;
	var timeStamp=json.timeStamp;
	var nonceStr=json.nonceStr;
	var package=json.package;
	var paySign=json.paySign;
	if(orderidss!=null&&orderidss!=""&orderidss!="undefined"&&
	appId!=null&&appId!=""&appId!="undefined"&&
	timeStamp!=null&&timeStamp!=""&timeStamp!="undefined"&&
	nonceStr!=null&&nonceStr!=""&nonceStr!="undefined"&&
	package!=null&&package!=""&package!="undefined"&&
	paySign!=null&&paySign!=""&paySign!="undefined"){
		WeixinJSBridge.invoke('getBrandWCPayRequest', {
	        "appId":appId,     //公众号名称，由商户传入     
	        "timeStamp":timeStamp,         //时间戳，自1970年以来的秒数     
	        "nonceStr":nonceStr, //随机串     
	        "package":package,     
	        "signType":"MD5",         //微信签名方式：     
	        "paySign":paySign //微信签名 
	    }
		, function(res) {
			// 使用以下方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回 ok，但并不保证它绝对可靠
			if (res.err_msg == "get_brand_wcpay_request:ok") {
	//			alert("支付成功");
	//			var chack=chackDB();
	//			alert(chack);
	//			if(chack==2){
				window.location.href = rootPath+"/artadmissions/choice.html";
	//			}
				// window.location.href =
				// "${pageContext.request.contextPath}/wx/success?orderId=${orderId}";
			} else if (res.err_msg == "get_brand_wcpay_request:fail") {
				close(1,orderidss);
			} else if (res.err_msg == "get_brand_wcpay_request:cancel") {
				close(2,orderidss);
			} else {
				alert("错误信息：" + res.err_msg);
			}
		});
	}
}

function close(type,orderidss){
	$.ajax({
	    url : rootPath+"/close",
	    type : "get",
	    cache : false,
	    async : false,
	    data:{
	    	orderid:orderidss
	    },
	    beforeSend:function(xhr){  
          img.show().css({   
              "position": "fixed",   
              "top": "40%",  
              "left": "45%",  
              "margin-top": function () { return -1 * img.height() / 2; },    
              "margin-left": function () { return -1 * img.width() / 2; }   
          });   
          mask.show().css("opacity", "0.1");           
        },
	    success:function(){
	    	if(type==1){
	    		alert('支付失败');
	    	}else if(type==2){
	    		alert('支付取消');
	    	}
	    	window.location.href = rootPath+"/artadmissions/choice.html";
	    },
	    complete:function(xhr){  
                        img.hide();  
                        mask.hide();           
              
        }
	});
}

var openid=utils.getParam("openid");

function topay(studentid){
	$.ajax({
	    url : rootPath+"/weixinpay",
	    type : "get",
	    data : {
	    	openid:openid,
	    	studentid:studentid
	    },
	    cache : false,
	    async : false,
	    success:function(result){
			if (typeof WeixinJSBridge == "undefined") {
				if (document.addEventListener) {
					document.addEventListener('WeixinJSBridgeReady',onBridgeReady, false);
				} else if (document.attachEvent) {
					document.attachEvent('WeixinJSBridgeReady',onBridgeReady);
					document.attachEvent('onWeixinJSBridgeReady',onBridgeReady);
				}
			} else {
				onBridgeReady(result);
			}
	    }
	});
}